import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:new_yuqib_app/yqbConfig/custom_font_style.dart';
import 'package:new_yuqib_app/yqbConfig/yqb_toast.dart';
import 'package:new_yuqib_app/yqbNetwork/api_metholds.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

class FeedbackController extends GetxController {
  RxList<AssetEntity> uploadImages = <AssetEntity>[].obs;

  final controller = TextEditingController();

  showAgreementDialog() async {
    final state = await Permission.storage.isGranted;
    if (!state) {
      Get.dialog(
        Dialog(
          backgroundColor: Colors.white,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const SizedBox(height: 12),
              Text(
                "存储权限申请说明",
                style: CustomFontStyle.titleStyle(fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 16),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 12),
                child: Text(
                  "为了实现意见反馈中的图片上传功能，需要开启存储权限用于图片的上传",
                  style: CustomFontStyle.defaultStyle(size: 16),
                ),
              ),
              const SizedBox(height: 16),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  ElevatedButton(
                    onPressed: Get.back,
                    style: ElevatedButton.styleFrom(
                      backgroundColor: const Color(0XFFCFCFCF),
                      padding: const EdgeInsets.symmetric(horizontal: 24),
                    ),
                    child: Text(
                      "取消",
                      style: CustomFontStyle.defaultStyle(
                        size: 16,
                        color: Colors.white,
                      ),
                    ),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      Get.back();
                      selImages();
                    },
                    style: ElevatedButton.styleFrom(
                      padding: const EdgeInsets.symmetric(horizontal: 24),
                    ),
                    child: Text(
                      "确定",
                      style: CustomFontStyle.defaultStyle(size: 16, color: Colors.white),
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 12),
            ],
          ),
        ),
      );
    } else {
      selImages();
    }
  }

  ///图片选择器
  selImages() async {
    final state = await Permission.storage.request();
    if (state != PermissionStatus.granted) {
      EasyLoading.show(status: "您已经拒绝了该权限");
      return;
    } else {
      final result = await AssetPicker.pickAssets(
        Get.context!,
        pickerConfig: AssetPickerConfig(
          selectedAssets: uploadImages,
          maxAssets: 9,
          requestType: RequestType.image,
        ),
      );
      if (result != null) {
        uploadImages.clear();
        uploadImages.addAll(result);
      }
    }
  }

  ///图片上传
  imageFileUpload() async {
    if (uploadImages.isEmpty) {
      feedback();
      return;
    }
    final imageUrls = <String>[];
    EasyLoading.show(status: "图片上传中...");
    for (final asset in uploadImages) {
      final path = await asset.file;
      final res = await ApiMethods.uploadImage(path!.path);
      if (res.isNotEmpty) imageUrls.add(res);
    }
    EasyLoading.dismiss();
    feedback(imageUrls: imageUrls);
  }

  ///意见反馈
  feedback({List<String>? imageUrls}) {
    final data = {"content": controller.text, "attach": imageUrls?.join(",") ?? ""};
    EasyLoading.show(status: "提交中...");
    ApiMethods.feedback(data).then((value) {
      EasyLoading.dismiss();
      YqbToast.show( "提交成功，感谢您的反馈");
      Get.back();
    }).catchError((err) {
      EasyLoading.dismiss();
      YqbToast.show("提交失败，请检查后重试");
    });
  }
}
